2  Kapitel 2: Datenaufbereitung

2.1 Pakete installieren und laden

if (!require("pacman")) install.packages("pacman")
Lade nötiges Paket: pacman
pacman::p_load(haven, psych,
               sjmisc, sjPlot, writexl,
               tidyverse, multilevelTools)

2.2 Daten einlesen

Mit load() können wir .RData Dateien einlesen (für weitergehende Infos s. auch Einführung in R, Kapitel 3.2.4.)

load("../data/df_cfa_wide.RData")

2.3 Daten ansehen

Der Datensatz hat 131 Spalten (ncol()) und 100 Zeilen (nrow()) (eine pro Person).

Wie wir mit names() sehen, gibt es die Variablen id für die Personidentifikation (jede Person hat ihre eigene Nummer), a1-a5, b1-b5, und c1-c3. Da jeder Tag (1-10 Tage) von jeder Variable seine eigene Spalte bekommt (t1-t10) gibt es viele Spalten und wir nennen das Datenformat daher breites Datenformat ( wide format).

a,b und c bilden jeweils eine Skala mit 5 bzw. bei c 3 Indikatoren.

Mit head() können wir einen Blick in die Daten werfen.

ncol(df_cfa_wide)
[1] 131
nrow(df_cfa_wide)
[1] 100
names(df_cfa_wide)
  [1] "id"     "a1_t1"  "a1_t2"  "a1_t3"  "a1_t4"  "a1_t5"  "a1_t6"  "a1_t7" 
  [9] "a1_t8"  "a1_t9"  "a1_t10" "a2_t1"  "a2_t2"  "a2_t3"  "a2_t4"  "a2_t5" 
 [17] "a2_t6"  "a2_t7"  "a2_t8"  "a2_t9"  "a2_t10" "a3_t1"  "a3_t2"  "a3_t3" 
 [25] "a3_t4"  "a3_t5"  "a3_t6"  "a3_t7"  "a3_t8"  "a3_t9"  "a3_t10" "a4_t1" 
 [33] "a4_t2"  "a4_t3"  "a4_t4"  "a4_t5"  "a4_t6"  "a4_t7"  "a4_t8"  "a4_t9" 
 [41] "a4_t10" "a5_t1"  "a5_t2"  "a5_t3"  "a5_t4"  "a5_t5"  "a5_t6"  "a5_t7" 
 [49] "a5_t8"  "a5_t9"  "a5_t10" "b1_t1"  "b1_t2"  "b1_t3"  "b1_t4"  "b1_t5" 
 [57] "b1_t6"  "b1_t7"  "b1_t8"  "b1_t9"  "b1_t10" "b2_t1"  "b2_t2"  "b2_t3" 
 [65] "b2_t4"  "b2_t5"  "b2_t6"  "b2_t7"  "b2_t8"  "b2_t9"  "b2_t10" "b3_t1" 
 [73] "b3_t2"  "b3_t3"  "b3_t4"  "b3_t5"  "b3_t6"  "b3_t7"  "b3_t8"  "b3_t9" 
 [81] "b3_t10" "b4_t1"  "b4_t2"  "b4_t3"  "b4_t4"  "b4_t5"  "b4_t6"  "b4_t7" 
 [89] "b4_t8"  "b4_t9"  "b4_t10" "b5_t1"  "b5_t2"  "b5_t3"  "b5_t4"  "b5_t5" 
 [97] "b5_t6"  "b5_t7"  "b5_t8"  "b5_t9"  "b5_t10" "c1_t1"  "c1_t2"  "c1_t3" 
[105] "c1_t4"  "c1_t5"  "c1_t6"  "c1_t7"  "c1_t8"  "c1_t9"  "c1_t10" "c2_t1" 
[113] "c2_t2"  "c2_t3"  "c2_t4"  "c2_t5"  "c2_t6"  "c2_t7"  "c2_t8"  "c2_t9" 
[121] "c2_t10" "c3_t1"  "c3_t2"  "c3_t3"  "c3_t4"  "c3_t5"  "c3_t6"  "c3_t7" 
[129] "c3_t8"  "c3_t9"  "c3_t10"
head(df_cfa_wide)
id a1_t1 a1_t2 a1_t3 a1_t4 a1_t5 a1_t6 a1_t7 a1_t8 a1_t9 a1_t10 a2_t1 a2_t2 a2_t3 a2_t4 a2_t5 a2_t6 a2_t7 a2_t8 a2_t9 a2_t10 a3_t1 a3_t2 a3_t3 a3_t4 a3_t5 a3_t6 a3_t7 a3_t8 a3_t9 a3_t10 a4_t1 a4_t2 a4_t3 a4_t4 a4_t5 a4_t6 a4_t7 a4_t8 a4_t9 a4_t10 a5_t1 a5_t2 a5_t3 a5_t4 a5_t5 a5_t6 a5_t7 a5_t8 a5_t9 a5_t10 b1_t1 b1_t2 b1_t3 b1_t4 b1_t5 b1_t6 b1_t7 b1_t8 b1_t9 b1_t10 b2_t1 b2_t2 b2_t3 b2_t4 b2_t5 b2_t6 b2_t7 b2_t8 b2_t9 b2_t10 b3_t1 b3_t2 b3_t3 b3_t4 b3_t5 b3_t6 b3_t7 b3_t8 b3_t9 b3_t10 b4_t1 b4_t2 b4_t3 b4_t4 b4_t5 b4_t6 b4_t7 b4_t8 b4_t9 b4_t10 b5_t1 b5_t2 b5_t3 b5_t4 b5_t5 b5_t6 b5_t7 b5_t8 b5_t9 b5_t10 c1_t1 c1_t2 c1_t3 c1_t4 c1_t5 c1_t6 c1_t7 c1_t8 c1_t9 c1_t10 c2_t1 c2_t2 c2_t3 c2_t4 c2_t5 c2_t6 c2_t7 c2_t8 c2_t9 c2_t10 c3_t1 c3_t2 c3_t3 c3_t4 c3_t5 c3_t6 c3_t7 c3_t8 c3_t9 c3_t10
1 3.336252 2.149828 1.613518 3.351085 3.320900 4.173775 3.653771 1.926106 5.621661 4.948758 3.380463 5.110309 3.004133 3.852294 3.694431 5.658948 4.973751 4.999227 5.123906 6.090460 3.223528 5.678413 2.503233 4.972103 4.703663 3.273027 3.187604 3.664482 5.464105 3.765162 4.399347 2.765556 1.898476 2.612551 3.476859 4.192700 2.856953 4.112356 4.579488 3.134414 3.542602 2.387270 2.807670 3.233779 4.210036 3.565438 3.293944 3.909548 5.635526 4.620410 3.370896 2.486919 0.991958 2.248097 1.953658 1.544111 2.103968 1.763424 3.087552 1.618470 1.697640 0.952220 -0.685685 0.004118 0.675750 -0.373186 1.938302 0.447036 3.860272 1.211009 0.584167 2.101517 0.003147 0.711264 0.921623 1.303905 1.201749 0.899882 1.797242 0.734580 1.576649 0.331000 -0.517730 2.185949 0.284884 1.687402 1.388842 0.160632 2.144261 1.205662 2.847195 2.552073 1.955846 2.028044 2.361978 2.539967 2.125510 1.206909 4.844395 4.320178 4.325797 3.364003 2.582169 2.918614 2.829237 4.141474 2.711044 4.787052 4.594232 2.511473 1.030847 5.711729 2.659834 3.163276 3.910432 5.234767 5.515933 5.206142 4.431722 4.953160 3.822098 3.613752 2.155132 2.463234 2.739723 3.409232 3.488225 1.453246 4.117747 3.745528
2 3.707018 2.377418 1.854018 4.632683 2.479164 2.234632 4.485121 4.011457 2.427658 2.600831 3.996684 3.074568 3.716554 3.796472 3.108752 2.922636 4.654462 4.431057 3.497468 4.130753 0.810690 2.874266 3.669414 2.478778 2.406462 2.515853 3.129976 3.718570 2.542158 2.606330 3.515638 4.128262 3.577047 4.258751 4.238509 3.267878 4.119021 2.712250 2.601381 5.293957 2.907960 2.834590 3.827659 5.278379 3.967381 4.134327 5.923659 4.251589 3.292468 3.639972 2.323243 2.042416 2.601619 0.235322 1.990456 3.388960 0.995331 3.325457 2.207927 0.221311 2.990608 1.476811 2.349784 3.097647 2.047705 2.722032 3.112804 1.368452 2.251611 1.587188 1.013232 2.926646 2.095343 2.858416 3.019904 1.726097 3.515165 1.348090 1.890474 2.311975 1.190393 2.184150 1.549624 0.452129 1.513757 1.432716 1.950500 2.007023 0.249291 -0.504312 2.010378 4.018477 3.410691 3.063017 3.897992 2.469268 2.906900 2.914951 0.608605 2.239330 2.576924 1.208644 2.606353 3.886131 3.856964 3.332155 0.597512 2.731819 2.110466 2.687502 -1.710033 -0.670529 -0.330814 -0.601643 -0.251286 0.439045 0.734464 0.066167 -0.849054 -0.757472 0.351828 0.095795 0.897598 2.615174 0.455632 2.211615 -0.235576 0.389909 -0.160126 1.833746
3 5.177071 -0.128053 2.797632 0.303081 4.314881 2.337009 3.575595 1.818855 -0.306364 1.614770 2.958702 2.735971 2.978813 4.107814 4.661105 3.033462 2.912859 3.754350 1.986925 2.453018 5.346752 4.501127 4.825683 4.908117 5.724673 5.153263 4.766531 5.198076 1.471673 3.829653 4.772178 2.755005 4.204091 2.631057 4.744125 3.983818 2.875329 3.684137 1.252706 1.649124 5.782683 4.648184 3.675335 5.918703 5.729757 5.027148 4.950459 5.555384 2.874046 2.074345 3.034341 5.163917 1.010752 1.666880 1.295466 1.477020 1.009490 1.175458 1.284057 3.434230 4.263902 3.116927 2.598624 2.281766 4.053404 2.417430 1.154442 2.357182 -0.103946 2.599341 3.981286 1.433954 2.329805 3.582351 2.509821 -0.341738 1.558373 2.687843 1.337345 3.732375 3.988386 2.509786 2.058585 3.740040 3.256067 3.500106 0.782067 3.601466 0.922850 4.635307 3.024556 1.150083 3.052157 3.163149 2.655710 0.093386 -0.073910 2.227515 1.712568 2.898763 2.820979 1.664689 2.688626 2.657114 1.381627 2.417342 1.443182 2.776111 2.248535 2.684371 1.748407 2.869058 1.052509 3.174517 0.118054 0.789280 2.679447 0.740977 1.135104 0.980832 1.537544 2.950929 1.429920 2.534687 3.792833 1.161559 0.105494 2.915895 1.714465 2.815621
4 4.705740 3.597286 2.948942 2.098839 2.783596 4.420878 1.358347 3.848268 3.714631 2.284839 4.038396 4.916627 2.739167 3.144930 2.224095 2.968036 2.365223 3.665594 2.585768 3.208773 5.110747 4.059650 3.327694 4.411642 3.937307 5.154507 3.126869 4.554551 3.766840 4.039097 5.965881 5.027371 4.130869 5.175652 4.037358 5.245027 3.205133 3.004498 6.786242 4.499281 3.958800 3.838211 3.211688 2.475119 2.191741 2.052676 2.687921 2.079803 3.929772 1.621818 3.621642 3.197431 2.838179 1.280215 1.746913 2.267367 1.597599 2.441468 1.211542 4.138991 1.342898 1.821522 1.688056 2.463308 2.030884 0.815056 0.953910 1.729489 1.332197 3.093290 2.400477 2.873127 1.997939 1.791812 -0.960833 0.893946 0.647417 1.110916 1.748169 2.121472 1.952886 2.904973 0.878772 1.269630 0.835469 -1.070358 -0.541286 1.063053 1.485312 3.607293 4.027168 3.804839 2.787383 2.807136 2.706166 2.527843 1.675010 2.839636 3.124758 4.327591 -0.571315 2.285224 1.057377 1.803651 0.992669 1.980404 3.310448 1.662047 2.537811 1.245361 1.490392 0.456182 1.328019 1.697168 0.693560 0.252673 2.728439 1.103803 1.332902 0.163830 0.043749 0.744745 0.204956 -0.370906 -1.022587 -0.403501 1.095947 0.274934 1.170815 -1.658070
5 3.949976 3.330208 5.413867 3.719810 5.798861 3.088709 4.012137 5.875070 3.181456 4.931577 3.317796 5.810359 5.343310 3.928448 3.798973 3.932334 2.986737 6.926680 4.932457 6.716081 3.673230 3.927261 4.515421 3.791699 4.846026 3.398835 2.273474 4.371252 1.762524 3.275910 5.841838 8.072948 6.721671 6.987111 5.944237 4.489158 4.921571 7.603350 6.165390 6.983403 3.389670 5.893949 3.749111 4.411794 2.873486 2.611568 1.286889 4.646997 3.107310 4.433750 1.701253 0.278520 3.249563 2.181162 3.107517 0.092424 1.985821 1.740418 3.187945 2.244310 3.361174 3.497546 3.372372 2.430237 5.397523 3.270945 3.053623 1.919141 3.688502 2.961518 4.337413 3.496237 5.441549 3.444545 4.486709 4.455020 2.470650 5.844872 4.583409 6.108861 4.302733 2.764837 3.876676 3.527337 5.128605 2.163503 1.886539 1.734753 3.437857 3.740089 4.179862 3.775715 5.299768 2.953386 3.807893 2.537854 2.901724 4.785203 5.926126 4.928503 3.053878 1.885364 4.448834 3.900540 4.728735 2.129385 3.805631 2.089345 3.743529 2.992289 1.790360 1.515488 3.618075 3.781413 4.205560 1.303834 3.673459 3.327604 3.160806 3.722098 1.817841 2.362278 2.812572 4.554119 3.287976 3.279677 1.435943 3.242930 0.500366 5.079764
6 2.274255 2.290222 1.376191 2.173916 2.099371 3.312339 2.172030 2.595873 2.289308 1.580778 2.787734 2.067549 2.180852 3.302369 1.863362 3.760236 2.294264 2.135892 1.550506 2.432511 3.703579 2.047107 3.065654 1.978181 2.963593 2.955959 3.468359 4.343268 3.525407 3.837417 4.151001 0.835873 1.884775 2.608722 3.012065 2.776904 2.578840 3.399579 3.577152 3.039595 1.512579 0.621510 1.092427 1.911605 1.368582 2.511084 0.839038 3.272104 1.121144 1.740897 2.996994 -1.267884 2.586945 2.184757 -0.124601 2.601747 1.747253 2.520691 4.051763 2.738223 1.022146 -1.129770 1.219736 -0.032099 2.843606 -1.122089 0.739502 2.138073 0.272516 0.625661 2.134281 0.231331 2.861858 1.303966 1.199804 1.300905 1.312581 2.266003 3.106999 2.869239 2.308207 1.068449 2.414964 1.894137 2.681821 0.512435 2.125864 2.787195 2.595406 2.191860 2.267998 0.203488 2.181351 1.670791 0.222494 0.947280 2.464313 2.733245 2.599860 1.042442 1.648781 0.598068 0.225724 0.521410 1.669001 1.384766 2.299040 2.014711 2.088084 0.651604 2.356883 0.718648 1.309239 2.011107 1.617811 3.844703 1.838274 2.215482 2.364317 1.437971 2.745956 3.067538 3.096565 2.927631 4.206480 3.969140 5.377585 2.509150 2.817378 1.073844

2.4 Daten transformieren: Langformat

Als erstes transformieren wir die Daten vom Breit- ins Langformat, so dass jede Messung (Tag 1-Tag 10) eine eigene Zeile bekommt. Diese Variable nennen wir “time”. Im ersten Schritt machen wir mit pivot_longer() den Datensatz seeehr lang, es bekommt nämlich jede Messung von jeder Variable ihre eigenen Zeile. Wir machen den Datensatz dann im zweiten Schritt mit pivot_wider() wieder etwas breiter mit dem Ziel, eine Zeile pro Person und Tag zu bekommen, und jeweils eine Spalte pro Item.

Die Funktionsweise von pivot_longer() und pivot_wider() ist in der Einführung zu R, Kapitel 4.3 ausführlicher beschrieben.

df_cfa_superlong <- df_cfa_wide |> 
  pivot_longer(
    cols = -id, # All columns except id
    names_to = c("variable", "time"), # Namensgebende Spalten sollen heissen: variable, time
    names_sep = "_t" 
    # Namensgebende Spalten anhand "_t_ separieren (z.B. "a1_t1" --> gespalten in "a1" und "1", "_t" ist der Separator)
  ) 

head(df_cfa_superlong)
id variable time value
1 a1 1 3.336252
1 a1 2 2.149828
1 a1 3 1.613518
1 a1 4 3.351085
1 a1 5 3.320900
1 a1 6 4.173775
df_cfa_long <- df_cfa_superlong |> 
  pivot_wider(names_from = variable, #namen aus variable
              values_from = value) |>  # werte aus "value" (Standardname)
  mutate(time = as.numeric(time)) # time ist eine Zahl von 1-10, wurde aber zuvor als Character-Vector abgespeichert

head(df_cfa_long)
id time a1 a2 a3 a4 a5 b1 b2 b3 b4 b5 c1 c2 c3
1 1 3.336252 3.380463 3.223528 4.399347 3.542602 3.370896 1.697640 0.584167 1.576649 2.847195 4.325797 1.030847 3.822098
1 2 2.149828 5.110309 5.678413 2.765556 2.387270 2.486919 0.952220 2.101517 0.331000 2.552073 3.364003 5.711729 3.613752
1 3 1.613518 3.004133 2.503233 1.898476 2.807670 0.991958 -0.685685 0.003147 -0.517730 1.955846 2.582169 2.659834 2.155132
1 4 3.351085 3.852294 4.972103 2.612551 3.233779 2.248097 0.004118 0.711264 2.185949 2.028044 2.918614 3.163276 2.463234
1 5 3.320900 3.694431 4.703663 3.476859 4.210036 1.953658 0.675750 0.921623 0.284884 2.361978 2.829237 3.910432 2.739723
1 6 4.173775 5.658948 3.273027 4.192700 3.565438 1.544111 -0.373186 1.303905 1.687402 2.539967 4.141474 5.234767 3.409232

Das Ergebnis:

  • eine Zeile pro Person (id) und Messung (time). Die Spalten id und time identifizieren also für jede Zeile, von welcher Person und welchem Tag die Werte in den folgenden Spalten kommen.
  • eine Spalte pro Variable (a1-a5, b1-b5, c1-c3)

2.5 Daten transformieren: Skalenscores erstellen

Als nächstes können wir mittels summarise() die Skalenscores erstellen. Wir verwenden eine simple Form der Skalenerstellung bei dem der Mittelwert aller vorhandenen Items einer Skala rowMeans(...) verwendet wird.

df_cfa_long_scores <- df_cfa_long |> group_by(id, time) |> 
  summarise(
    a = rowMeans(across(starts_with("a")), na.rm = TRUE),
    b = rowMeans(across(starts_with("b")), na.rm = TRUE),
    c = rowMeans(across(starts_with("c")), na.rm = TRUE),
    .groups = "drop" # group_by() wieder aufheben für den finalen Datensatz
  )

(Best-practice ist es bei fehlenden Daten genau hinzuschauen und nur dann einen Skalenwert zu erstellen, wenn die Personen zu einen gewissen Prozentsatz aller Items eine Antwort geben (etwa 2/3). Eine solche Funktion könnten wir programmieren, lassen es aber für das Beispiel weg.)

2.6 Daten transformieren: Zentrierung

Für die spätere Verwendung zerlegen wir die Rohvariablen mittels person-mean Zentrierung. Wir zentrieren wir die Skalenvariablen, die täglich gemessen werden (aber nicht Baseline-Variablen), mittels de_mean(). de_mean() nimmt als Argumente (a) mit Komma getrennte Namen der Variablen, die wir zentrieren wollen (mehrere auf einmal ist möglich), (b) mittels grp = Argument die identifizierende Variable für die Gruppenzugehörigkeit in Anführungszeichen ("id".

df_cfa_long_scores <- df_cfa_long_scores |> 
  de_mean(a,b, c, grp = "id")
head(df_cfa_long_scores)
id time a b c a_dm b_dm c_dm a_gm b_gm c_gm
1 1 3.576438 2.0153094 3.059581 -0.2566380 0.4476044 -0.5267811 3.833076 1.567705 3.586362
1 2 3.618275 1.6847458 4.229828 -0.2148012 0.1170408 0.6434662 3.833076 1.567705 3.586362
1 3 2.365406 0.3495072 2.465712 -1.4676704 -1.2181978 -1.1206501 3.833076 1.567705 3.586362
1 4 3.604362 1.4354944 2.848375 -0.2287140 -0.1322106 -0.7379871 3.833076 1.567705 3.586362
1 5 3.881178 1.2395786 3.159797 0.0481014 -0.3281264 -0.4265645 3.833076 1.567705 3.586362
1 6 4.172778 1.3404398 4.261824 0.3397012 -0.2272652 0.6754625 3.833076 1.567705 3.586362

Als Ergebnis erhalten wir die zusätzlichen Variablen a, b, c jeweils mit “_dm” und “_gm”. Was verbirgt sich dahinter? Wir haben einen Datensatz mit den unzentrierten / Rohvariablen der Skalen (ohne Suffix), den zentrierten Variablen (Suffix _dm) und den Mittelwerten der Personen (Suffix _gm), den wir zur weiteren Verwendung auch abspeichern.

Damit ist die Transformation der Daten abgeschlossen! Wir können die Datensätze - “df_cfa_long” für die Items und “df_cfa_long_scores” für die Skalen nun abspeichern.

save(df_cfa_long, df_cfa_long_scores, file = "../data/df_cfa_long.RData")

2.7 Überprüfe dein Verständnis

beispiel_zentrierung <- df_cfa_long_scores |> 
  filter(id == 1) |> 
  select(id, time, a, a_dm, a_gm)

beispiel_zentrierung
id time a a_dm a_gm
1 1 3.576438 -0.2566380 3.833076
1 2 3.618275 -0.2148012 3.833076
1 3 2.365406 -1.4676704 3.833076
1 4 3.604362 -0.2287140 3.833076
1 5 3.881178 0.0481014 3.833076
1 6 4.172778 0.3397012 3.833076
1 7 3.593205 -0.2398718 3.833076
1 8 3.722344 -0.1107326 3.833076
1 9 5.284937 1.4518608 3.833076
1 10 4.511841 0.6787644 3.833076

Warum ist der Wert, den Person 1 in “a_gm” hat, in jeder Zeile gleich, nicht aber bei “a_dm” und “a”? Wie müsste man “a” transformieren, damit man auf “a_gm” und “a_dm” kommt? Denke an die mathematischen Operationen die du in mutate() eingeben müsstest, wie Plus, Minus ( +, -, /, …) und die Funktion für Mittelwert ( mean()).

Die Variable a ist die Rohvariable, die den gemessenen Wert auf der Skala an jedem Tag angibt. Variablen mit “_gm” und “_dm” werden von sjmisc::de_mean() erstellt. Variablen mit “_gm” stehen für den Mittelwert der Person über alle Tage hinweg. Variablen mit “_dm” stehen für die täglichen Abweichungen vom Mittelwert der Person. “a_dm” ergibt sich aus: \(a = a - a\_gm\). Wir können dies auch in R replizieren mit:

beispiel_zentrierung |> 
  mutate(a_gm_manuell = mean(a),
         a_dm_manuell = a - a_gm_manuell)
id time a a_dm a_gm a_gm_manuell a_dm_manuell
1 1 3.576438 -0.2566380 3.833076 3.833076 -0.2566380
1 2 3.618275 -0.2148012 3.833076 3.833076 -0.2148012
1 3 2.365406 -1.4676704 3.833076 3.833076 -1.4676704
1 4 3.604362 -0.2287140 3.833076 3.833076 -0.2287140
1 5 3.881178 0.0481014 3.833076 3.833076 0.0481014
1 6 4.172778 0.3397012 3.833076 3.833076 0.3397012
1 7 3.593205 -0.2398718 3.833076 3.833076 -0.2398718
1 8 3.722344 -0.1107326 3.833076 3.833076 -0.1107326
1 9 5.284937 1.4518608 3.833076 3.833076 1.4518608
1 10 4.511841 0.6787644 3.833076 3.833076 0.6787644

2.8 Übung Datenaufbereitung

Schau dir den Datensatz df_cfa_exercise an und repliziere die Schritte oben:

  1. Daten in Langformat transformieren und als df_uebung_lang <- zuweisen.
  2. Skalenscores für x und y erstellen und als df_uebung_lang_scores <- zuweisen.
  3. Skalenscores für x und y zentrieren (weiterhin in df_uebung_lang_scores).

Du kannst dazu den Code von oben wiederverwenden und auf den hier verwendeten Datensatz und seine Variablen anpassen.

load("../data/df_cfa_exercise.RData")
head(df_cfa_exercise)
id x1_t1 x1_t2 x1_t3 x1_t4 x1_t5 x1_t6 x1_t7 x1_t8 x1_t9 x1_t10 x2_t1 x2_t2 x2_t3 x2_t4 x2_t5 x2_t6 x2_t7 x2_t8 x2_t9 x2_t10 x3_t1 x3_t2 x3_t3 x3_t4 x3_t5 x3_t6 x3_t7 x3_t8 x3_t9 x3_t10 x4_t1 x4_t2 x4_t3 x4_t4 x4_t5 x4_t6 x4_t7 x4_t8 x4_t9 x4_t10 x5_t1 x5_t2 x5_t3 x5_t4 x5_t5 x5_t6 x5_t7 x5_t8 x5_t9 x5_t10 y1_t1 y1_t2 y1_t3 y1_t4 y1_t5 y1_t6 y1_t7 y1_t8 y1_t9 y1_t10 y2_t1 y2_t2 y2_t3 y2_t4 y2_t5 y2_t6 y2_t7 y2_t8 y2_t9 y2_t10 y3_t1 y3_t2 y3_t3 y3_t4 y3_t5 y3_t6 y3_t7 y3_t8 y3_t9 y3_t10 y4_t1 y4_t2 y4_t3 y4_t4 y4_t5 y4_t6 y4_t7 y4_t8 y4_t9 y4_t10 y5_t1 y5_t2 y5_t3 y5_t4 y5_t5 y5_t6 y5_t7 y5_t8 y5_t9 y5_t10
1 4.927017 3.484359 5.209319 3.777327 2.822955 2.874990 4.399279 3.999338 2.829229 4.534257 3.569244 3.716360 4.025572 2.601702 4.917407 4.417907 5.836183 2.584864 2.416726 3.428733 3.123268 2.200183 2.416466 2.600930 2.179303 3.370835 5.833981 3.574207 2.399534 1.756436 5.759150 4.865658 5.106690 4.142291 4.984553 4.902819 7.088255 6.024145 4.119966 4.134776 4.700258 3.579269 5.881804 4.111085 4.600078 4.055922 4.496880 4.373719 4.322336 3.912015 4.329297 3.597471 3.552542 3.052100 2.592308 3.082656 4.184377 4.064089 3.642032 2.777390 4.477198 4.454955 4.760682 3.288172 5.678996 4.679951 4.928690 5.166207 3.485148 3.454632 2.848577 5.551165 3.769263 3.649328 2.271035 5.264214 3.832915 3.875455 2.525079 1.163129 4.678402 4.682683 3.607138 4.363976 3.401883 5.139966 5.210060 4.713772 3.424733 2.631818 3.480544 3.221823 3.853132 3.120772 3.842651 2.815425 3.273848 3.126467 3.647887 3.895862
2 5.024072 2.470240 2.259750 3.383971 3.405628 2.925394 3.722530 3.104064 4.068848 2.923166 3.661601 3.732475 2.084769 3.978517 2.855392 3.381118 3.865699 3.911571 3.102266 2.858461 5.186708 1.540100 3.453976 1.671304 4.397883 4.996236 3.609226 2.266511 5.332266 5.041631 2.577130 1.182058 2.648178 2.580964 4.378121 2.015839 2.477425 2.718602 2.512741 3.104794 2.425656 2.302399 1.261294 2.294336 0.776553 3.169805 2.850572 2.683466 2.756426 3.199966 3.488302 3.348598 1.567981 2.689270 2.093631 0.946151 2.760766 1.974521 0.788326 1.546632 3.613495 3.002835 2.618250 3.474268 4.107470 3.950412 3.461374 3.162900 2.504398 3.957139 3.269670 2.924702 2.951385 1.403080 1.747677 3.671060 2.755273 2.860291 3.582403 2.855433 4.296437 4.459933 3.877955 3.542746 4.623289 5.768592 2.820453 4.347125 5.008321 5.628651 3.446137 3.354074 0.216899 1.984132 2.551348 1.010641 3.200017 1.600360 1.541323 3.561905
3 3.661946 4.515898 2.627455 2.097116 3.295632 4.674215 3.854248 3.430167 3.423922 4.346313 3.369146 2.583655 2.805899 2.629365 2.759572 2.753171 1.063016 2.180655 3.397097 3.797167 3.784757 3.774945 1.163201 2.880376 2.329117 5.686030 3.881392 3.907517 3.697921 3.270770 2.040071 4.645036 2.312931 2.106188 3.517961 3.099783 3.312862 4.134669 3.522531 2.951239 3.113105 2.513375 2.887626 4.867711 4.787919 2.084980 4.140326 3.036390 4.304286 3.375258 1.956426 2.710257 2.345528 2.025919 1.763310 1.487463 1.435715 3.255219 3.430917 2.560493 2.972647 3.841999 2.321647 5.547948 2.755463 3.135102 1.252126 1.455243 0.126571 0.754453 3.451330 2.634979 2.051891 3.677636 4.382111 0.630656 2.520796 2.908761 -0.251480 1.706827 4.334217 3.310795 3.634877 3.788040 3.997747 3.418240 1.451858 5.332129 3.107325 2.796257 3.441736 1.732713 4.678564 3.527282 4.910696 3.960887 2.782757 3.427556 4.629581 4.084214
4 1.933604 1.835414 4.246882 3.229524 2.456309 2.818070 2.602155 3.215303 4.200392 2.272876 0.706912 -0.801259 1.640656 1.587674 1.470820 2.430678 1.819882 1.695119 1.338284 0.605444 0.133074 0.158037 1.792941 1.790514 1.269408 3.353523 2.179509 1.409529 0.888444 1.224938 0.326793 0.174798 2.335327 1.525558 -0.955707 3.610542 1.960530 -0.865120 1.385375 1.430312 0.685265 -0.230309 0.633548 1.199429 0.310757 2.375445 1.869323 0.641068 1.674369 1.870899 3.087726 0.985564 0.784520 2.725897 0.850097 0.685824 1.168844 -0.239602 2.799497 2.131838 1.596182 1.458913 3.630159 2.387079 0.462230 0.650202 2.356301 0.755215 2.026497 2.441231 2.790539 2.575200 3.509842 3.116568 3.452843 3.093494 1.836035 2.222184 2.761235 4.237117 3.075454 1.881486 2.553940 1.045851 3.202694 4.161503 2.702880 0.817310 2.733675 1.369864 0.687845 2.150121 1.293763 0.737558 0.793559 1.293628 1.312178 -1.925199 0.168729 1.409099
5 2.274807 2.908011 3.179267 2.941155 3.710268 2.717757 3.510822 1.550497 2.728217 4.947419 4.312907 1.624997 3.450676 3.296389 4.639991 4.428850 4.773291 2.273139 2.820446 3.868664 4.675249 2.412898 3.691474 4.295843 4.288994 3.679321 3.664403 3.449730 4.627416 5.374093 3.114936 0.937293 3.231832 2.031238 2.033217 0.704127 1.766480 1.422281 1.951835 3.429262 2.336261 3.616157 3.631813 3.258421 5.976209 4.089613 2.896231 4.596477 4.129096 7.093438 3.593002 3.190479 2.211968 2.500292 3.999353 2.826291 2.768957 2.979500 3.155079 3.074007 2.688911 2.680939 4.108605 2.347372 3.353903 3.417996 3.602248 3.722665 1.867266 4.398309 2.672452 3.234202 4.512234 1.011391 2.736290 2.495329 2.283781 0.979636 2.101990 3.646597 4.168885 6.011056 5.576807 4.452524 5.109755 3.539751 5.203918 6.884093 6.758122 4.835822 1.016782 1.853578 2.498811 3.096292 2.572159 2.496271 1.515257 1.183772 3.329594 2.744476
6 3.184660 4.046734 3.540150 4.059281 2.374465 2.067087 3.390182 3.492470 3.661657 1.892471 2.910669 2.351059 0.726541 1.928325 -0.062322 1.306896 0.272615 1.463914 2.076900 2.569456 5.019716 2.328710 2.837884 4.043339 1.947459 2.449519 3.065598 4.321089 2.036927 1.389769 3.537501 4.318936 1.631771 3.685972 3.609319 1.645394 2.917829 2.317283 2.267146 3.963144 3.536972 3.295575 3.075710 3.460675 3.224478 1.660497 3.093049 2.421162 2.188736 3.557481 0.309582 1.857645 1.819382 1.085073 2.427644 1.988539 2.246552 2.295530 1.075097 1.388195 1.656496 1.752528 2.418624 1.340024 0.824050 0.698072 1.970136 2.064690 1.795599 4.439921 1.967216 0.477582 1.303589 0.223835 1.302689 1.254710 2.383013 0.978524 2.745955 1.923409 1.194165 1.793615 0.785669 0.374648 1.989626 3.421168 2.360255 0.441367 2.033105 3.299585 1.283497 1.845475 1.331906 1.425406 2.937331 0.823984 2.306800 2.113772 2.127941 1.507769
# 1. Daten in Langformat transformieren - funktionen: pivot_longer(), pivot_wider()


# 2. Skalenscores für X und Y erstellen - funktionen: group_by(), summarise()

# 3. Skalenscores für X und Y zentrieren - Funktionen: de_mean()
load("../data/df_cfa_exercise.RData")
head(df_cfa_exercise)
id x1_t1 x1_t2 x1_t3 x1_t4 x1_t5 x1_t6 x1_t7 x1_t8 x1_t9 x1_t10 x2_t1 x2_t2 x2_t3 x2_t4 x2_t5 x2_t6 x2_t7 x2_t8 x2_t9 x2_t10 x3_t1 x3_t2 x3_t3 x3_t4 x3_t5 x3_t6 x3_t7 x3_t8 x3_t9 x3_t10 x4_t1 x4_t2 x4_t3 x4_t4 x4_t5 x4_t6 x4_t7 x4_t8 x4_t9 x4_t10 x5_t1 x5_t2 x5_t3 x5_t4 x5_t5 x5_t6 x5_t7 x5_t8 x5_t9 x5_t10 y1_t1 y1_t2 y1_t3 y1_t4 y1_t5 y1_t6 y1_t7 y1_t8 y1_t9 y1_t10 y2_t1 y2_t2 y2_t3 y2_t4 y2_t5 y2_t6 y2_t7 y2_t8 y2_t9 y2_t10 y3_t1 y3_t2 y3_t3 y3_t4 y3_t5 y3_t6 y3_t7 y3_t8 y3_t9 y3_t10 y4_t1 y4_t2 y4_t3 y4_t4 y4_t5 y4_t6 y4_t7 y4_t8 y4_t9 y4_t10 y5_t1 y5_t2 y5_t3 y5_t4 y5_t5 y5_t6 y5_t7 y5_t8 y5_t9 y5_t10
1 4.927017 3.484359 5.209319 3.777327 2.822955 2.874990 4.399279 3.999338 2.829229 4.534257 3.569244 3.716360 4.025572 2.601702 4.917407 4.417907 5.836183 2.584864 2.416726 3.428733 3.123268 2.200183 2.416466 2.600930 2.179303 3.370835 5.833981 3.574207 2.399534 1.756436 5.759150 4.865658 5.106690 4.142291 4.984553 4.902819 7.088255 6.024145 4.119966 4.134776 4.700258 3.579269 5.881804 4.111085 4.600078 4.055922 4.496880 4.373719 4.322336 3.912015 4.329297 3.597471 3.552542 3.052100 2.592308 3.082656 4.184377 4.064089 3.642032 2.777390 4.477198 4.454955 4.760682 3.288172 5.678996 4.679951 4.928690 5.166207 3.485148 3.454632 2.848577 5.551165 3.769263 3.649328 2.271035 5.264214 3.832915 3.875455 2.525079 1.163129 4.678402 4.682683 3.607138 4.363976 3.401883 5.139966 5.210060 4.713772 3.424733 2.631818 3.480544 3.221823 3.853132 3.120772 3.842651 2.815425 3.273848 3.126467 3.647887 3.895862
2 5.024072 2.470240 2.259750 3.383971 3.405628 2.925394 3.722530 3.104064 4.068848 2.923166 3.661601 3.732475 2.084769 3.978517 2.855392 3.381118 3.865699 3.911571 3.102266 2.858461 5.186708 1.540100 3.453976 1.671304 4.397883 4.996236 3.609226 2.266511 5.332266 5.041631 2.577130 1.182058 2.648178 2.580964 4.378121 2.015839 2.477425 2.718602 2.512741 3.104794 2.425656 2.302399 1.261294 2.294336 0.776553 3.169805 2.850572 2.683466 2.756426 3.199966 3.488302 3.348598 1.567981 2.689270 2.093631 0.946151 2.760766 1.974521 0.788326 1.546632 3.613495 3.002835 2.618250 3.474268 4.107470 3.950412 3.461374 3.162900 2.504398 3.957139 3.269670 2.924702 2.951385 1.403080 1.747677 3.671060 2.755273 2.860291 3.582403 2.855433 4.296437 4.459933 3.877955 3.542746 4.623289 5.768592 2.820453 4.347125 5.008321 5.628651 3.446137 3.354074 0.216899 1.984132 2.551348 1.010641 3.200017 1.600360 1.541323 3.561905
3 3.661946 4.515898 2.627455 2.097116 3.295632 4.674215 3.854248 3.430167 3.423922 4.346313 3.369146 2.583655 2.805899 2.629365 2.759572 2.753171 1.063016 2.180655 3.397097 3.797167 3.784757 3.774945 1.163201 2.880376 2.329117 5.686030 3.881392 3.907517 3.697921 3.270770 2.040071 4.645036 2.312931 2.106188 3.517961 3.099783 3.312862 4.134669 3.522531 2.951239 3.113105 2.513375 2.887626 4.867711 4.787919 2.084980 4.140326 3.036390 4.304286 3.375258 1.956426 2.710257 2.345528 2.025919 1.763310 1.487463 1.435715 3.255219 3.430917 2.560493 2.972647 3.841999 2.321647 5.547948 2.755463 3.135102 1.252126 1.455243 0.126571 0.754453 3.451330 2.634979 2.051891 3.677636 4.382111 0.630656 2.520796 2.908761 -0.251480 1.706827 4.334217 3.310795 3.634877 3.788040 3.997747 3.418240 1.451858 5.332129 3.107325 2.796257 3.441736 1.732713 4.678564 3.527282 4.910696 3.960887 2.782757 3.427556 4.629581 4.084214
4 1.933604 1.835414 4.246882 3.229524 2.456309 2.818070 2.602155 3.215303 4.200392 2.272876 0.706912 -0.801259 1.640656 1.587674 1.470820 2.430678 1.819882 1.695119 1.338284 0.605444 0.133074 0.158037 1.792941 1.790514 1.269408 3.353523 2.179509 1.409529 0.888444 1.224938 0.326793 0.174798 2.335327 1.525558 -0.955707 3.610542 1.960530 -0.865120 1.385375 1.430312 0.685265 -0.230309 0.633548 1.199429 0.310757 2.375445 1.869323 0.641068 1.674369 1.870899 3.087726 0.985564 0.784520 2.725897 0.850097 0.685824 1.168844 -0.239602 2.799497 2.131838 1.596182 1.458913 3.630159 2.387079 0.462230 0.650202 2.356301 0.755215 2.026497 2.441231 2.790539 2.575200 3.509842 3.116568 3.452843 3.093494 1.836035 2.222184 2.761235 4.237117 3.075454 1.881486 2.553940 1.045851 3.202694 4.161503 2.702880 0.817310 2.733675 1.369864 0.687845 2.150121 1.293763 0.737558 0.793559 1.293628 1.312178 -1.925199 0.168729 1.409099
5 2.274807 2.908011 3.179267 2.941155 3.710268 2.717757 3.510822 1.550497 2.728217 4.947419 4.312907 1.624997 3.450676 3.296389 4.639991 4.428850 4.773291 2.273139 2.820446 3.868664 4.675249 2.412898 3.691474 4.295843 4.288994 3.679321 3.664403 3.449730 4.627416 5.374093 3.114936 0.937293 3.231832 2.031238 2.033217 0.704127 1.766480 1.422281 1.951835 3.429262 2.336261 3.616157 3.631813 3.258421 5.976209 4.089613 2.896231 4.596477 4.129096 7.093438 3.593002 3.190479 2.211968 2.500292 3.999353 2.826291 2.768957 2.979500 3.155079 3.074007 2.688911 2.680939 4.108605 2.347372 3.353903 3.417996 3.602248 3.722665 1.867266 4.398309 2.672452 3.234202 4.512234 1.011391 2.736290 2.495329 2.283781 0.979636 2.101990 3.646597 4.168885 6.011056 5.576807 4.452524 5.109755 3.539751 5.203918 6.884093 6.758122 4.835822 1.016782 1.853578 2.498811 3.096292 2.572159 2.496271 1.515257 1.183772 3.329594 2.744476
6 3.184660 4.046734 3.540150 4.059281 2.374465 2.067087 3.390182 3.492470 3.661657 1.892471 2.910669 2.351059 0.726541 1.928325 -0.062322 1.306896 0.272615 1.463914 2.076900 2.569456 5.019716 2.328710 2.837884 4.043339 1.947459 2.449519 3.065598 4.321089 2.036927 1.389769 3.537501 4.318936 1.631771 3.685972 3.609319 1.645394 2.917829 2.317283 2.267146 3.963144 3.536972 3.295575 3.075710 3.460675 3.224478 1.660497 3.093049 2.421162 2.188736 3.557481 0.309582 1.857645 1.819382 1.085073 2.427644 1.988539 2.246552 2.295530 1.075097 1.388195 1.656496 1.752528 2.418624 1.340024 0.824050 0.698072 1.970136 2.064690 1.795599 4.439921 1.967216 0.477582 1.303589 0.223835 1.302689 1.254710 2.383013 0.978524 2.745955 1.923409 1.194165 1.793615 0.785669 0.374648 1.989626 3.421168 2.360255 0.441367 2.033105 3.299585 1.283497 1.845475 1.331906 1.425406 2.937331 0.823984 2.306800 2.113772 2.127941 1.507769
# 1. Daten in Langformat transformieren - funktionen: pivot_longer(), pivot_wider()
df_uebung_superlang <- df_cfa_exercise |> 
  pivot_longer(
    cols = -id, # All columns except id
    names_to = c("variable", "time"),
    names_sep = "_t"
  ) 
df_uebung_lang <- df_uebung_superlang |> 
  pivot_wider(names_from = variable,
              values_from = value) |> 
  mutate(time = as.numeric(time)) 

# 2. Skalenscores für X und Y erstellen - funktionen: group_by(), summarise()
df_uebung_lang_scores <- df_uebung_lang |> group_by(id, time) |> 
  summarise(
    x = rowMeans(across(starts_with("x")), na.rm = TRUE),
    y = rowMeans(across(starts_with("y")), na.rm = TRUE),
    .groups = "drop" # group_by() wieder aufheben für den finalen Datensatz
  )

# 3. Skalenscores für X und Y zentrieren - Funktionen: de_mean()
df_uebung_lang_scores <- df_uebung_lang_scores |> 
  de_mean(x, y, grp = "id")


head(df_uebung_lang_scores)
id time x y x_dm y_dm x_gm y_gm
1 1 4.415787 3.962804 0.3959958 0.1601657 4.019792 3.802638
1 2 3.569166 4.301619 -0.4506258 0.4989815 4.019792 3.802638
1 3 4.527970 3.908551 0.5081786 0.1059135 4.019792 3.802638
1 4 3.446667 3.494870 -0.5731246 -0.3077683 4.019792 3.802638
1 5 3.900859 3.557375 -0.1189324 -0.2452633 4.019792 3.802638
1 6 3.924495 4.196442 -0.0952970 0.3938045 4.019792 3.802638

2.9 Abspeichern der gebildeten Skalen

Zum Schluss speichern wir die Ergebnisse (sowohl die Items als auch die Skalen in Langformat) der Übung ab.

save(df_uebung_lang, df_uebung_lang_scores, file = "../data/df_uebung.RData")